function varargout=plcol(fileName, scanNum, varargin) 

% PLCOL read data in *.dat file generated at APS(9ID-B) 
% Return N-columns data
% *****************************
% Input arguments: 
% fileName: Name of *.dat file
% scanNum: A scan number (number)
% colName: Name of column(s) (string)
% ******************************
% Output arguments:
% A matrix of N columns
% IF the column name is not found, data in that column are NaN, not zeros.
% ******************************
% Example:
% fileName = '*.dat';
% [data] = expcol(fileName,1,'q1','iof','siof');
% q1 = data(:,1);
% iof = data(:,2);
% siof = data(:,3);
% To plot data: 
% ploterr(q1,iof,[],siof,'o','logy','hhy',0.3)
% ******************************
% H. Zhang @ Jan 29, 2016
% ******************************

if exist(fileName, 'file')
    % read the file as string
    content = fileread(fileName);
    % add a newline at the end of the string to read all the scans 
    % including the last one 
    content(end+1) = char(10);
    % parses data by the scan of interest
    scanNum = num2str(scanNum);
    pattern = strcat('\#S\s',scanNum,'\s(.*?)\n+\n');
    scan = regexp(content, pattern, 'match', 'all' ) ;
    % if the scan number is not found
    if ~isempty(scan)
        % remove all the labels except headers of this scan
        scanData = regexp(scan{1}, '\#L\s(.*?)\n+\n','match','all');
        data = textscan(scanData{1},'%s','Delimiter','#'); %
        header = data{1}{2};
        header = textscan(header,'%s');
        
        colNum = length(varargin); % the total number of columns
        colHead = [];
        for j = 1:colNum
            colHead(j) = 0;
        end

        for k = 2:length(header{1}) % start with the second one
            for j = 1:colNum
                if strcmp(header{1}{k},varargin(j)) %find the column of interest
                    colHead(j) = k-1;
                end
            end
        end


        % export data
        dataExp = [];
        for i = 5:length(data{1})
            row = sscanf(data{1}{i},'%f');
            if isempty(row)
                break
            else
                for j = 1:colNum
                    if colHead(j) ~= 0 && colHead(j) <= length(row)
                        dataExp(i-4,j) = row(colHead(j)); %save the data points
                    else
%                         dataExp(i-4,j) = NaN; %if the column cannot be found
                        colHead(j) = 0;
                        break
                    end
                end
            end
        end
        
        % if the input column is not found
        for j = 1:colNum
            if colHead(j) == 0
                disp(strcat('Warning! Cannot find Column:',varargin(j)));
                break
            end
        end
        
        varargout{1}=dataExp;
    else % if the scan number is not found
        disp(strcat('Warning! Cannot find Scan Number:', scanNum));
        varargout{1}=[];
    end
else % if the file is not found
    disp(strcat('Warning! Cannot find File:', fileName));
    varargout{1}=[];
end
return
